package com.dommy.tab.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dommy.tab.MainActivity;
import com.dommy.tab.R;
import com.dommy.tab.pojo.User;
import com.dommy.tab.util.ImgUtil;
import com.dommy.tab.util.MyHttpObject;
import com.dommy.tab.util.MyHttpUtil;
import com.dommy.tab.util.MyUrlUtil;
import com.dommy.tab.util.UserUtil;
import com.dommy.tab.util.WaterUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 设置页
 */
public class MeFragment extends Fragment {
    TextView Water_four_userName,Water_four_Id;
    ImageView Water_four_Img;
//    待补充
    LinearLayout Water_four_zhifu,Water_four_xiaoxi,Water_four_daiding1,Water_four_daiding2,
            Water_four_daiding3,Water_four_shezhi;
    ArrayList<User> list = new ArrayList<>();
    Bitmap bitmap;
    View view;
    public MeFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        init();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        init(view);
        return view;
    }
    public void init(View view){
        Water_four_userName = view.findViewById(R.id.Water_four_userName);
        Water_four_Id = view.findViewById(R.id.Water_four_Id);
        Water_four_Img = view.findViewById(R.id.Water_four_Img);
        Water_four_zhifu = view.findViewById(R.id.Water_four_zhifu);
        Water_four_xiaoxi = view.findViewById(R.id.Water_four_xiaoxi);
        Water_four_daiding1 = view.findViewById(R.id.Water_four_daiding1);
        Water_four_daiding2 = view.findViewById(R.id.Water_four_daiding2);
        Water_four_daiding3 = view.findViewById(R.id.Water_four_daiding3);
        Water_four_shezhi = view.findViewById(R.id.Water_four_shezhi);
//        发送数据请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(list == null || list.size() == 0){
                    MyHttpObject<User> myHttpObject = new MyHttpObject<>();
                    myHttpObject.setList(MyUrlUtil.SELECT_USER+ UserUtil.USER_ID,User.class);
                    list = myHttpObject.getList();
                }
                if(bitmap == null){
                    bitmap = ImgUtil.getImg(MyUrlUtil.SELECT_IMG1);
                }
                handler.sendEmptyMessageDelayed(1,100);
            }
        }).start();
        setListener();
    }

    public Handler handler = new  Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                setView();
            }
        }
    };

    public void setView(){
        Water_four_userName.setText(list.get(0).getUserName());
        Water_four_Id.setText(" id: "+list.get(0).getId()+"");
        Water_four_Img.setImageBitmap(bitmap);
    }

    public void setListener(){
        Water_four_zhifu.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_zhifu");
            }
        });

        Water_four_xiaoxi.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_xiaoxi");
            }
        });

        Water_four_daiding1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_daiding1");
            }
        });

        Water_four_daiding2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_daiding2");
            }
        });

        Water_four_daiding3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_daiding3");
            }
        });

        Water_four_shezhi.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println(" Water_four_shezhi");
                UserUtil.USER = null;
//                WaterUtil.WATER_LIST = new ArrayList<>();
                WaterUtil.FLAG = false;
                MsgFragment.FLAG =false;
                Intent intent = new Intent(getActivity(),LoginActivity.class);

                startActivity(intent);
            }
        });
    }
}
